package main

import (
	"database/sql"
	"database/sql/driver"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"strconv"
	"strings"
	"time"
)

func main()  {
	strQuery := "SELECT BIZ_ORDER_ID,ID from pay_purchase where id >"

	strEnd := " order by id limit 1000;"



	dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s","root","","tcp","10.204.6.100",4000,"pay")

	DB,_ := sql.Open("mysql",dsn)
	var i int64 =0
	for  {
		str := strQuery+strconv.FormatInt(i,10)+strEnd
		rows,_ :=DB.Query(str)
		if !rows.Next(){
			fmt.Println("game over!")
			return
		}
		for rows.Next(){
			var orderId string
			rows.Scan(&orderId,&i)
			query := "SELECT ID, PERSONAL_ACCOUNT_ID, FC_RELATION_ID, FC_RELATION_ID_TYPE, ENTERPRISE_ACCOUNT_ID, MODEL_TYPE_CODE, BUSSINE_TYPE, BUSSINE_TYPE_DESC, PAY_TYPE, BIZ_ORDER_ID, MESSAGE_ID, PURCHASE_AMOUNT, REASON, PURCHASE_CHANNEL, PAY_PERSONAL_ACCOUNT_ID, PAY_FC_RELATION_ID, PAY_FC_RELATION_ID_TYPE, CREATED_TIME, STATUS, NOTIFY_TIME, CHANNEL_ORDER_ID, CLASSNAME, NOTIFY_TOPIC, REMAIN_AMOUNT, EXT1, TRANS_AMOUNT FROM pay_purchase WHERE BIZ_ORDER_ID = '"+orderId+"' AND MODEL_TYPE_CODE = 11 AND STATUS IN (2) ORDER BY ID DESC LIMIT 1"
			begin1 := time.Now().Unix()
			_,err1 :=DB.Exec(query)
			end1 := time.Now().Unix()
			stamp := end1- begin1
			if end1-begin1 > 2{
				fmt.Println("execute time is:",stamp,query,time.Now())
			}
			if err1 != nil {
				if  err1 == driver.ErrBadConn || strings.Contains(err1.Error(),"close"){
					DB.Close()
					DB,_ = sql.Open("mysql",dsn)
					continue
				}
				fmt.Println("make an error.",stamp,query,time.Now(),err1)
			}
		}
		rows.Close()
	}

	DB.Close()
}
